package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;



@Slf4j
@RestController
@RequestMapping("/employee")
//员工表现层
public class EmployeeController {
    @Value("${reggie.admin.user}")
    private String adminUser;
    @Autowired
    private EmployeeService employeeService;
    //登录
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest requ){
        String username = employee.getUsername();
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> lab= Wrappers.lambdaQuery();
        lab.eq(Employee::getUsername,username);
        Employee emp = employeeService.getOne(lab);
        if (emp==null){
            return R.error("账号或者密码错误");
        }
        if (!emp.getPassword().equals(password)){
            return R.error("账号或者密码错误");
        }
        if (emp.getStatus()==0){
            return R.error("该用户已被禁用");
        }
        requ.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }
    //登出
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest requ){
        requ.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest requ,@RequestBody Employee employee){
        //打印新增员工的日志信息
        log.info("新增员工，员工信息：{}",employee.toString());
        //设置初始密码123456，进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //新增
        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //打印接收的分页日志
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行查询
        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    //禁用启用
    @PutMapping
    public R<String> update(HttpServletRequest requ,@RequestBody Employee employee){
        //打印本次接收的对象日志
        log.info(employee.toString());
        Employee empid = employeeService.getById(employee.getId());
        if (adminUser.equals(empid.getUsername())){
            return R.error("不允许修改管理员");
        }
        //修改
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
    }

    //根据id查询员工数据
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        //打印根据id查询
        log.info("根据id查询员工信息...id:{}",id);
        //根据id修改
        Employee employee = employeeService.getById(id);
        //如果有对象就传出
        if (employee!=null){
            return R.success(employee);
        }
        //没有对象就返回没有查询到
        return R.error("没有查询到对应员工信息");
    }
}
